package com.ihisun.tmhcms.core.service.activityrotationservice.impl;

import com.ihisun.tmhcms.core.dao.activityrotationdao.ActivityRotationDao;
import com.ihisun.tmhcms.core.dto.activityrotationdto.ActivityRotationDto;
import com.ihisun.tmhcms.core.service.activityrotationservice.ActivityRotationService;
import com.ihisun.tmhcms.utils.DateUtils;
import com.ihisun.tmhcms.utils.ResponseVo;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Date;
import java.util.Optional;

@Service
public class ActivityRotationServiceImpl implements ActivityRotationService {
    @Resource
    ActivityRotationDao activityRotationDao;
    @Override
    public ResponseVo editActivityRotation(ActivityRotationDto activityRotationDto) {

        if (activityRotationDto.getId()!=null&&!"".equals(activityRotationDto)){
            activityRotationDto.setUpdateTime(DateUtils.getCurrentTime());
            activityRotationDao.save(activityRotationDto);
        }else{
            activityRotationDto.setState(1);
            activityRotationDto.setUpdateTime(DateUtils.getCurrentTime());
            activityRotationDto.setCreateTime(DateUtils.getCurrentTime());
            activityRotationDao.save(activityRotationDto);
        }

        return ResponseVo.ok();
    }

    @Override
    public ResponseVo updateActivityRotationState(String id, Integer state) {
        Optional<ActivityRotationDto> byId = activityRotationDao.findById(Long.parseLong(id));
        ActivityRotationDto activityRotationDto = byId.get();
        activityRotationDto.setState(state);
        activityRotationDto.setUpdateTime(DateUtils.getCurrentTime());
        activityRotationDao.save(activityRotationDto);
        return ResponseVo.ok();
    }
}
